package com.zhong.subject.domain.handler.subject;

import com.google.common.base.Preconditions;
import com.zhong.subject.common.enums.IsDeleteFlagEnum;
import com.zhong.subject.common.enums.SubjectInfoTypeEnum;
import com.zhong.subject.domain.bo.SubjectAnswerBO;
import com.zhong.subject.domain.bo.SubjectInfoBO;
import com.zhong.subject.domain.bo.SubjectOptionBO;
import com.zhong.subject.domain.convert.SubjectJudgeConverter;
import com.zhong.subject.infra.basic.entity.SubjectJudge;
import com.zhong.subject.infra.basic.service.SubjectJudgeService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 判断题目的策略类
 */
@Component
public class JudgeTypeHandler implements SubjectTypeHandler {

    @Resource
    private SubjectJudgeService subjectJudgeService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.JUDGE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //判断题目插入
        SubjectJudge subjectJudge = new SubjectJudge();
        SubjectAnswerBO subjectAnswerBO = subjectInfoBO.getOptionList().get(0);
        Preconditions.checkNotNull(subjectAnswerBO,"JudgeTypeHandler.add.error: 没有找到第一个选项");
        subjectJudge.setSubjectId(subjectInfoBO.getId());
        subjectJudge.setIsCorrect(subjectAnswerBO.getIsCorrect());
        subjectJudge.setIsDelete(IsDeleteFlagEnum.UN_DELETE.getCode());
        subjectJudgeService.insert(subjectJudge);
    }

    @Override
    public SubjectOptionBO query(Long subjectId) {
        //判断题目查询
        SubjectJudge subjectJudge = new SubjectJudge();
        subjectJudge.setSubjectId(subjectId);
        List<SubjectJudge> subjectJudgeList = subjectJudgeService.queryByCondition(subjectJudge);
        List<SubjectAnswerBO> subjectAnswerBOList = SubjectJudgeConverter.INSTANCE.covertJudgeListToAnswerBoList(subjectJudgeList);

        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;
    }
}
